package com.xxcxcxcx.xc_crop.mixin;

import com.xxcxcxcx.xc_crop.common.Configuration;
import com.xxcxcxcx.xc_crop.common.ReadConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.vehicle.Minecart;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.ArrayList;

@Mixin(CropBlock.class)
public abstract class WheatMixin extends BlockBehaviour implements ReadConfig {
    public WheatMixin(Properties p_60452_) {
        super(p_60452_);
    }
    private String ConfigKey = "wheat";
    private ArrayList<String> biomeWhiteList = new ArrayList<>();
    private boolean whitelistRead = false;//保证config只读一次
    private boolean canBiome(BlockPos pos, LevelReader levelReader){
        Biome.BiomeCategory currentBiome = Biome.getBiomeCategory(levelReader.getBiome(pos));
        if(!whitelistRead)ReadConfigMessage();
        return biomeWhiteList.isEmpty()||biomeWhiteList.contains(currentBiome.getName());
    }
    private void ReadConfigMessage(){
        biomeWhiteList = Configuration.whitelistStrtoArraylist(Configuration.Config.get(ConfigKey));
        whitelistRead = true;
    }
    @Inject(method = "canSurvive", at = @At("RETURN"),cancellable = true)
    public void respawn(BlockState p_52282_, LevelReader p_52283_, BlockPos p_52284_, CallbackInfoReturnable<Boolean> cir){
        if(p_52282_.getBlock().getClass() == CropBlock.class){
            cir.setReturnValue(cir.getReturnValueZ()&&p_52283_.canSeeSky(p_52284_)&&canBiome(p_52284_,p_52283_));
        }
    }
}
